package cn.tedu.springmvc.Controller;

import cn.tedu.springmvc.mapper.UserMapper;
import cn.tedu.springmvc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping
    public String getUserAll(){
        List<User> list = userMapper.getUserAll();
        return list.toString();
    }

    @PostMapping("updataUser")
    public String updataUser(@RequestBody User user){
        int row = userMapper.updataUser(user);
        return row==1?"修改成功":"修改失败";
    }

    @PostMapping("/adduser")
    public String addUser(@RequestBody User user){
        int row= userMapper.putUser(user);
        return row==1?"添加成功":"添加失败";
    }

    @GetMapping("/getUserById/{id}")
    public String getUserById(@PathVariable int id){
        System.out.println(id);
        User user = userMapper.getUserById(id);
        return user.toString();
    }


    @PostMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable int id){
        int row = userMapper.deleteUser(id);
        return row==1?"删除成功":"删除失败";

    }


}
